$(function(){
	var departmentUrl = '';
	var departmentShow ='';
	department = {
		//添加记录
		add : function(){
			departmentUrl = 'system/department_add_action.php';
			departmentShow = '新增';
			$('#department').dialog('open').dialog('center').dialog('setTitle','新增部门').dialog('iconCls','icon-add');
			//$('input[name="username"]').focus();
		},
		//删除记录
		remove : function(){
			var rows = $('#departmentdg').datagrid('getSelections');
			if(rows.length > 0){
				$.messager.confirm('确定操作','您正的要删除所选的记录吗？',function(flag){
					if (flag){
						var ids = [];
						for (var i = 0; i < rows.length; i ++) {
							ids.push(rows[i].id);
						}
				
						$.ajax({
							type : 'POST',
							url : 'system/department_del_action.php',
							data : {
								ids : ids.join(','),
							},
							beforeSend : function () {
								$('#departmentdg').datagrid('loading');
							},
							success : function (data) {
								if (data) {
									$('#departmentdg').datagrid('loaded');
									$('#departmentdg').datagrid('load');
									$('#departmentdg').datagrid('unselectAll');
									$.messager.show({
										title : '提示',
										msg : data + '个部门被删除成功！',
									});
								}
							},
						});
					}
				});
			} else {
				$.messager.alert('警告','请选择要删除的记录！','info');
			}
		},
		edit : function(){
			departmentUrl = 'system/department_update_action.php';
			departmentShow = '修改'
			var rows = $('#departmentdg').datagrid('getSelections');
			if (rows.length > 1){
				$.messager.aler('警告操作','编辑记录只能选定一条数据！','warning');
			} else if (rows.length == 1){
				$.ajax({
					type : 'POST',
					url : 'system/department_get_action.php',
					data : {
						id : rows[0].id,
					},
					beforeSend : function(){
						$.messager.progress({
							text : '正在尝试获取数据...',
						});
					},
					success : function(data){
						$.messager.progress('close');
						if(data){
							var departmentobj = $.parseJSON(data);
							$('#department').form('load',{
								id : departmentobj[0].id,
								text : departmentobj[0].text,
								pid : departmentobj[0].pid,
							}).dialog('open').dialog('center').dialog('setTitle','修改部门').dialog('iconCls','icon-edit');
							
							$('#pid').combotree({
								url : 'menu.php',
								required : true,
								lines : true,
								//multiple : true,
								//checkbox : true,
								//onlyLeafCheck : true,
								onLoadSuccess : function (department, data) {
									var _this = this;
									if (data) {
										$(data).each(function (index, value) {
											if ($.inArray(value.text, pid) != -1) {
												$(_this).tree('check', value.target);
											}
											if (this.state == 'closed') {
												$(_this).tree('expandAll');
											}
										});
									}
								},
							});
						} else {
							$.messager.alert('获取失败','未知错误导致失败，请重试！','warning');
						}
					}
				})
			} else if (rows.length == 0){
				$.messager.alert('警告操作！','编辑记录至少选定一条数据！');
			}
		},
		search : function(){
			
		}
	}
	$('#departmentdg').datagrid({
		iconCls : 'icon-search',//标题图标			
		title : '部门列表',//标题名称		
		fit : true,//全屏
		border : false,//无边框	
		fitColumns : true,//列全屏		
		striped : true,//显示斑马线效果			
		rownumbers : true,//显示一个行号列		
		pagination : true,//分页
		pageSize : 10,//每页显示10条
		pageList : [10,20,30,40,50],//每页条数可选
		pageNumber : 1,//默认显示第1页
		sortName : 'id',//按X列排序
		sortOrder : 'desc',//排序类型
		//工具栏
		toolbar : [
			{
				iconCls:'icon-add',
				text:'增加',
				handler:function(){
					department.add();
				}
				
			},'-',{
				iconCls:'icon-edit',
				text:'修改',
				handler:function(){
					department.edit();
				}
				
			},'-',{
				iconCls:'icon-save',
				text:'删除',
				handler:function(){
					department.remove();
				}
			}
		],
		//内容表格
		columns : [[
			{field:'id',title:'id',checkbox:'true',align:'center',width:100,sortable:true,},
			{field:'text',title:'部门名称',align:'center',width:100,sortable:true,},
			{field:'pid',title:'上级部门',align:'center',width:100,sortable:true,},
			{field:'status',title:'状态',align:'center',width:100,sortable:true,
				formatter:function(value,row,index){
						if(value){
							return '启用';
						}else{
							return '禁用';
						}
				}
			},
			{field:'created_on',title:'创建时间',align:'center',width:100,sortable:true,},
		]],
		url : 'system/department_action.php',
	});
	
	$('#department').dialog({
		width : 350,
		modal : true,
		closed : true,
		//iconCls : 'icon-add',
		buttons : [{
			text : '提交',
			iconCls : 'icon-add-new',
			handler : function () {
				if ($('#department_add').form('validate')) {
					$.ajax({
						url : departmentUrl,
						type : 'POST',
						data : {
							id : $('#id').val(),
							text : $('#text').val(),
							pid : $('#pid').combotree('getValue'),
							status:$('#status').val(),
						},
						beforeSend : function () {
							$.messager.progress({
								text : '正在'+departmentShow+'中......',
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							if (data > 0) {
								$.messager.show({
									title : '提示',
									msg : departmentShow+'部门成功！',
								});
								$('#department').dialog('close').form('reset');
								$('#departmentdg').datagrid('reload');
							} else {
								$.messager.alert(departmentShow+'失败！','未知错误导致失败，请重试！','warning');
							}
						}
					});
				}
					
			},
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function (){
				$('#department').dialog('close').form('reset');
			},
		}],
	});
	
	//部门名称
	$('#department #text').validatebox({
		required : true,
		validType : 'length[2,20]',
		missingMessage : '请输入部门名称',
		invalidMessage : '部门名称在 2-20 位',
	});
	
	//上级部门
	$('#department #pid').combotree({
		url : 'system/department_getAll_action.php',
		required : true,
		lines : true,
		multiple : true,
		checkbox : true,
		onlyLeafCheck : true,
		onLoadSuccess : function (department, data) {
			var _this = this;
		if (data) {
				$(data).each(function (index, value) {
					if (this.state == 'closed') {
						$(_this).tree('expandAll');
					}
				});
			}
		},
	});
	
	
});